package cn.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.common.ExceResult;
import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.entity.civilass.CATempDetailEntity;
import cn.kpi.entity.civilass.CivilAssItemEntity;
import cn.kpi.entity.civilass.CivilAssTempEntity;
import cn.kpi.entity.sys.OrgEntity;
import cn.kpi.service.civilass.ICivilAssItemService;
import cn.kpi.service.civilass.ICivilAssTempService;
import cn.kpi.service.sys.IOrgService;

@Controller
@RequestMapping("civilassorgtemp")
public class CivilAssOrgTempController {
	
	@Autowired
	private IOrgService iOrgService;
	
	@Autowired
	private ICivilAssItemService iCivilAssItemService;
	
	@Autowired
	private ICivilAssTempService iCivilAssTempService;
	
	@RequestMapping("/civilass_org_temp_list")
	public String CivilAssOrgTempList(Model model) {
		return "civilass/civilass_org_temp_list";
	}
	
	@RequestMapping("/civilass_org_temp_edit")
	public String CivilAssOrgTempEdit(Model model) {
		return "civilass/civilass_org_temp_edit";
	}
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_list")
	public LayTable<CivilAssTempEntity> GetList(HttpServletRequest request) {

		List<CivilAssTempEntity> lst=null;
		LayTable<CivilAssTempEntity> layTable=new LayTable<CivilAssTempEntity>();
		try {
			String RoleName = request.getParameter("rolename");
			
			String where="";
			lst = iCivilAssTempService.GetCAItemByWhere(where);
//			for (int i = 0; i < lst.size(); i++) {
//				lst.get(i).setRowNum(i + 1);
//			}

			layTable.setCode(0);
			layTable.setCount(100);
			layTable.setData(lst);
			
		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}
	
	

	@RequestMapping(value = "/civilass_org_temp_show")
	public String GetOrgTempShowList(HttpServletRequest request,Model model,Long tempid) {

		try {
			
			List<CATempDetailEntity> lstdetail = iCivilAssTempService.GetItemListByTempId(tempid);
			String columns="";
			
			for(int i=0;i<lstdetail.size();i++){
				columns+="MAX(CASE WHEN itemname='"+lstdetail.get(i).getItemname()+"' THEN score ELSE 0 END) AS '"+lstdetail.get(i).getItemname()+"',";
			}
			columns=columns.substring(0,columns.length()-1);
			
			List<Map> lstorg=iCivilAssTempService.GetTempOrgListByTempId(tempid, columns);
			

			System.out.println(JSON.toJSONString(lstorg));
			
			model.addAttribute("titleh", lstdetail);
			model.addAttribute("databody", lstorg);
			
			
		} catch (Exception e) {
			
		}
		return "civilass/civilass_org_temp_show";
	}
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_org_list")
	public LayTable<OrgEntity> GetOrgList(HttpServletRequest request) {

		List<OrgEntity> lst=null;
		LayTable<OrgEntity> layTable=new LayTable<OrgEntity>();
		try {
			String RoleName = request.getParameter("rolename");
			
			
			lst = iOrgService.GetOrgList();
			for (int i = 0; i < lst.size(); i++) {
				lst.get(i).setRowNum(i + 1);
			}

			layTable.setCode(0);
			layTable.setCount(100);
			layTable.setData(lst);
			
		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_catitem_list")
	public LayTable<CivilAssItemEntity> GetCAItemList(HttpServletRequest request) {

		List<CivilAssItemEntity> lst=null;
		LayTable<CivilAssItemEntity> layTable=new LayTable<CivilAssItemEntity>();
		try {
			
			String where=" AND itemtype=0";
			
			
			lst = iCivilAssItemService.GetItemListByWhere(where);
			for (int i = 0; i < lst.size(); i++) {
				lst.get(i).setRowNum(i + 1);
			}

			layTable.setCode(0);
			layTable.setCount(100);
			layTable.setData(lst);
			
		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}
	
	@ResponseBody
	@RequestMapping(value = "/insert_temporg")
	public ExceResult InsertTempOrg(HttpServletRequest request,HttpServletResponse response){
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try{
		 String itemids=request.getParameter("itemids");
		 String orgids=request.getParameter("orgids");
		 String tempname=request.getParameter("tempname");
		 String[] itemidsArr=itemids.split(",");
		 String[] orgidsArr=orgids.split(",");
		 
		 List<Long> lstitem=new ArrayList<Long>();
		 for(int i=0;i<itemidsArr.length;i++){
			 lstitem.add(Long.parseLong(itemidsArr[i]));
		 }
		 
		 List<Long> lstorg=new ArrayList<Long>();
		 for(int i=0;i<orgidsArr.length;i++){
			 lstorg.add(Long.parseLong(orgidsArr[i]));
		 }
		 
		 CivilAssTempEntity entity=new CivilAssTempEntity();
		 entity.setTempname(tempname);
		 entity.setRemark("添加");
		 
		 String trim=orgids.substring(0, orgids.length()-1);
		 String trimitem=itemids.substring(0, itemids.length()-1);
		 
		 int iResult=iCivilAssTempService.InsertCivilAssTemp(entity, trimitem, lstorg, trim);
		 if(iResult>0){
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			}
			else{
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		}
		catch(Exception ex){
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
}
